package com.xmcowell.sportpacemaster.utils;

import android.content.Context;


import com.xmcowell.sportpacemaster.layout.entity.DaoMaster;
import com.xmcowell.sportpacemaster.layout.entity.DaoSession;
import com.xmcowell.sportpacemaster.layout.entity.TrainTimesTable;
import com.xmcowell.sportpacemaster.layout.entity.TrainTimesTableDao;
import com.xmcowell.sportpacemaster.layout.entity.UserInfo;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 1 创建数据库
 * 2 创建数据库表
 * 3 创建数据库的增删查改
 * 4 对数据库的升级
 * Created by admin on 2016/5/31.
 */
public class DaoManager {
    private static final String TAG = DaoManager.class.getSimpleName();
    private static final String DB_NAME = "mydb.sqlite";  //声明数据库
    private volatile static DaoManager manager = null; //多线程名称
    private static DaoMaster.DevOpenHelper helper;
    private static DaoMaster daomaster;
    private static DaoSession daoSession;
    private Context context;

    public void init(Context context) {
        this.context = context;
    }

    /**
     * 1 数据库类的创建
     * 线程安全创建DaoManager,使用单例模式获得操作数据库的对象
     *
     * @return note:为保证数据库的有效性，采用单利模式进行访问
     */
    public static DaoManager getInstance() {
        DaoManager instance = null;
        if (manager == null) {
            synchronized (DaoManager.class) {
                if (instance == null) {
                    instance = new DaoManager();
                    manager = instance;
                }
            }
        }
        return manager;
    }

    /**
     * 2 Master类的创建
     * 得到DaoMaster
     *
     * @return note：系统帮助用户监测是否有数据库，如果没有，则创建数据库
     */
    public DaoMaster getDaoMaster() {
        if (daomaster == null) {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
            daomaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daomaster;
    }

    /**
     * 3 数据库会话层DaoSession的创建
     * 完成对数据库的增删改查的操作，这里仅仅是一个接口。
     *
     * @return
     */
    public DaoSession getDaoSession() {
        if (daoSession == null) {
            if (daomaster == null) {
                daomaster = getDaoMaster();
            }
            daoSession = daomaster.newSession();
        }
        return daoSession;
    }

    /**
     * 关闭数据库的操作，使用完毕数据库，必须执行此操作。
     */
    public void CloseConnection() {
        CloseHelper();
        ColseDaoSession();
    }

    /**
     * 关闭helper
     */
    public void CloseHelper() {
        if (helper != null) {
            helper.close();
            helper = null;
        }
    }

    /**
     * 关闭Session会话层
     */
    public void ColseDaoSession() {
        if (daoSession != null) {
            daoSession.clear();
            daoSession = null;
        }
    }

    /**
     * 打开输出日志的操作
     */
    public void SetDebug() {
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
    }


    public void saveUserInfo(UserInfo mUserInfo) {
        clearUserInfo();
        getDaoSession().getUserInfoDao().save(mUserInfo);
    }

    public UserInfo getUserInfo() {
        List<UserInfo> list = getDaoSession().getUserInfoDao().queryBuilder().list();
        if (list.size() == 0) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public void clearUserInfo() {
        getDaoSession().getUserInfoDao().deleteAll();
    }

    /**
     * 获取从登录到现在的运动数据
     *
     * @param loginDate
     */
    public List<TrainTimesTable> getLoginToNowDate(String userid, Date loginDate, Date nowDate) {
        List<TrainTimesTable> result = new ArrayList<>();
        List<TrainTimesTable> list = DaoManager.getInstance().getDaoSession().getTrainTimesTableDao().queryBuilder()
                .where(TrainTimesTableDao.Properties.UserId.eq(userid)).list();
        for (TrainTimesTable table : list) {
            boolean b1 = table.getStartTime().getTime() - loginDate.getTime() > 0;
            boolean b2 = table.getStartTime().getTime() - nowDate.getTime() < 0;
            if (b1 && b2) {
                if (table.getDistance() != null && table.getDistance() > 0) {
                    result.add(table);
                }
            }
        }
        return result;
    }

}
